﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Taurus.Platform.DTO;
using System.Xml.Linq;

namespace Taurus.Platform.XML
{
    public class UserAccountPersistence:BasePersistence<UserAccount>
    {
        public override string FileName
        {
            get { return "UA.xml"; }
        }

        //public override void Update(UserAccount item)
        //{
        //    UserAccount original = GetObject(item.UID);
        //    Delete(item);
        //    if (original!=null && item.Password==string.Empty)
        //    {
        //        item.SmallAccount = original.SmallAccount;
        //        item.Password = original.Password;
        //        item.IsPublic = original.IsPublic;
        //        item.Friends = original.Friends;
        //        item.Email = original.Email;
        //    }
        //    Insert(item);
        //}

        public override XElement GetEl(UserAccount item)
        {
            var temp = from el in Root.Elements() where el.Attribute("UID").Value == item.UID select el;
            return temp.FirstOrDefault();
        }

        public override UserAccount GetObject(string id)
        {
            return XChange(GetEl(new UserAccount() { UID = id }));
        }

        protected override XElement XChange(UserAccount item)
        {
            XElement userEl = new XElement("user");
            userEl.SetAttributeValue("UID", item.UID);
            userEl.SetAttributeValue("IsPublic", item.IsPublic);
            userEl.Add(
                new XElement("Email", item.Email),
                new XElement("Name", item.Name),
                new XElement("Password", item.Password),
                new XElement("PicFullName", item.PicFullName),
                new XElement("Signature", item.Signature)
                );
            
            XElement saEl = new XElement("SA");
            foreach (var sa in item.SmallAccount)
            {
                XElement el = new XElement("SmallAccount");
                el.SetAttributeValue("UID", sa);
                saEl.Add(el);
            }
            userEl.Add(saEl);

            XElement groupEL = new XElement("GP");
            foreach (var group in item.Groups)
            {
                XElement el = new XElement("Group", group);
                groupEL.Add(el);
            }
            userEl.Add(groupEL);

            XElement friendEl = new XElement("Friends");
            foreach (var friend in item.Friends)
            {
                XElement el = new XElement("Friend");
                el.SetAttributeValue("UID", friend);
                friendEl.Add(el);
            }
            userEl.Add(friendEl);

            return userEl;
        }

        protected override UserAccount XChange(XElement item)
        {
            UserAccount ua = null;
            if (item!=null)
            {
                ua = new UserAccount();
                ua.UID = item.Attribute("UID").Value;
                ua.IsPublic = bool.Parse(item.Attribute("IsPublic").Value);
                ua.Email = item.Element("Email").Value;
                ua.Name = item.Element("Name").Value;
                ua.Password = item.Element("Password").Value;
                ua.PicFullName = item.Element("PicFullName").Value;
                ua.Signature = item.Element("Signature").Value;
                var t = from el in item.Element("GP").Elements("Group") select el.Value;
                ua.Groups = t.ToList();

                var bar = from el in item.Element("Friends").Elements("Friend") select el.Attribute("UID").Value;
                ua.Friends = bar.ToList();

                var foo = from el in item.Element("SA").Elements("SmallAccount") select el.Attribute("UID").Value;
                ua.SmallAccount = foo.ToList();
            }
            return ua;
        }
    }
}
